/*
	WindowEnhancement.h

	(c)2008 Palestar, Jeff Everett
*/

#define INTERFACE_DLL
#include "Debug/Assert.h"
#include "Interface/ButtonEnhancement.h"
#include "Interface/WindowEnhancement.h"
#include "Interface/GameDocument.h"
#include "Gui3d/WindowText.h"
#include "Game/NounShip.h"

//----------------------------------------------------------------------------

IMPLEMENT_FACTORY( WindowEnhancement, NodeWindow )

const int ENHANCEMENTS_MAX(12);

WindowEnhancement::WindowEnhancement()
{}

void WindowEnhancement::onUpdate( float t )
{
	GameDocument * pDoc = dynamic_cast<GameDocument *>( document() );

	NounShip *pShip = pDoc->ship();
	if(pShip)
	{
		// go through the enhancements attached to this ship and add the info to the 
		// enhancement window, so that when it is showed, players can view the details
		int nEnhCount = 0;
		// reset the totals
		for(int i=0;i<pShip->childCount();i++)
		{
			BaseNode * pChild = pShip->child(i);

			NounEnhancement *pEnh = WidgetCast<NounEnhancement>( pChild );
			if ( pEnh && nEnhCount < ENHANCEMENTS_MAX)
			{
				m_pEnhancementButtons[nEnhCount]->showWindow(true);
				m_pEnhancementButtons[nEnhCount]->setTip(CharString().format("<color;%6.6x>%s</color>",pEnh->color().RGB(),pEnh->GetName()));
				m_pEnhancementButtons[nEnhCount]->setEnhancement(pEnh);
				m_pEnhancementButtons[nEnhCount]->setEnhancementWindow(this);
				m_pEnhancementButtons[nEnhCount]->setIcon(pEnh->icon());
				nEnhCount ++;
			}
		}
		// make sure the rest of the buttons are hidden
		for(i=nEnhCount;i<ENHANCEMENTS_MAX;i++)
			m_pEnhancementButtons[i]->showWindow(false);
	}

	NodeWindow::onUpdate( t );
}

void WindowEnhancement::onActivate()
{
	NodeWindow::onActivate();

	for(int i=0;i<12;i++)
	{
		CharString buttonName;
		buttonName = CharString().format("ButtonEnh_%02d",i+1);
		m_pEnhancementButtons[i] = WidgetCast<ButtonEnhancement>( findNode( buttonName ) );
		// 
		m_pEnhancementButtons[i]->showWindow(false);
	}

	m_pWindowText = WidgetCast<WindowText>(findNode("InfoBox"));
	m_pRemoveEnhancementButton = WidgetCast<WindowButton>(findNode("ButtonRemoveEnhancement"));
	m_pSelectedButton = NULL;
}


//----------------------------------------------------------------------------

void WindowEnhancement::onRender( RenderContext & context, const RectInt & window )
{
	GameDocument * pDoc = dynamic_cast<GameDocument *>( document() );
	if(pDoc == NULL)
		return;
	NounShip *pShip = pDoc->ship();
	
	if(pShip)
	{
		if(m_pSelectedButton && m_pSelectedButton->enhancement())
		{
			CharString sDescription = m_pSelectedButton->enhancement()->GetDescription();
			m_pWindowText->setText(sDescription);
			m_pSelectedButton->renderGlow(context);
			m_pRemoveEnhancementButton->showWindow(true);
		}
		else
		{
			CharString sDescription = "<large><color;ffffffff>Totals</color></large>\n";
			sDescription += pShip->getEnhancementTotalDescription();
			m_pWindowText->setText(sDescription);
			m_pRemoveEnhancementButton->showWindow(false);
		}
	}
	NodeWindow::onRender(context,window);
}

bool WindowEnhancement::onButtonUp( const Message & msg )
{
	return true;
}

//----------------------------------------------------------------------------
// EOF
